﻿///////////////////////////////////////////////////////////
//  SinacorContextMenu.cs
//  Implementation of the Class SinacorContextMenu
//  Created on:      30-jul-2008 21:35
//  Original author: POliveira.7Comm
///////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls.Primitives;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    /// <summary>
    /// Componente de exibição do menu contextual
    /// </summary>
    public class SinacorContextMenu : System.Windows.Controls.ContextMenu
    {
		public SinacorContextMenu()
		{
			this.Opened += new RoutedEventHandler(SinacorContextMenu_Opened);
			this.Closed += new RoutedEventHandler(SinacorContextMenu_Closed);
			this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(SinacorContextMenu_IsVisibleChanged);
		}

		void SinacorContextMenu_Opened(object sender, RoutedEventArgs e)
		{
			this.TryToUpdateLayout();
		}

		void SinacorContextMenu_Closed(object sender, RoutedEventArgs e)
		{
			this.TryToUpdateLayout();
		}

		void SinacorContextMenu_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			this.TryToUpdateLayout();

			ContextMenu menu = sender as ContextMenu;
			Popup parentPopup = menu.Parent as Popup;
			if (parentPopup != null)
				parentPopup.PopupAnimation = PopupAnimation.None;
		}

        protected override System.Windows.DependencyObject GetContainerForItemOverride()
        {
            return new SinacorContextMenuItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is SinacorContextMenuItem) || 
                   (item is SinacorContextMenuItemSeparator) || 
                   (item is Panel);
        }

		void TryToUpdateLayout()
		{
			try{ this.UpdateLayout(); }
			catch{}
		}
    }
}